<template>
  <div class="app-container">
    <!-- 搜索框 -->
    <div style="margin-top: 15px">
      <el-input
        placeholder="请输入要添加的分类，左边选择该分类的等级"
        v-model="input"
        class="input-with-select blogAddInput"
      >
        <el-select v-model="select" slot="prepend" placeholder="请选择">
          <el-option label="1" value="1"></el-option>
          <el-option label="2" value="2"></el-option>
          <el-option label="3" value="3"></el-option>
          <el-option label="4" value="4"></el-option>
          <el-option label="5" value="5"></el-option>
        </el-select>
      </el-input>
      <!-- 添加按钮 -->
      <el-button type="primary" @click="addBlogTypeHandle()">添加</el-button>
    </div>
    <!-- 表格数据区 -->
    <el-table :data="data" border style="width: 100%">
      <!-- 序号 -->
      <el-table-column prop="id" label="序号" width="100" align="center">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <!-- 博客类别 -->
      <el-table-column prop="title" label="博客类别" align="center">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <!-- 文章数量 -->
      <el-table-column prop="address" label="文章数量" align="center">
        <template slot-scope="scope">{{ scope.row.articleCount }}</template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作" align="center" width="150px">
        <template slot-scope="scope">
          <!-- 编辑 -->
          <el-tooltip
            class="item"
            effect="dark"
            content="编辑"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="editBlogTypeHandle(scope.row)"
            ></el-button>
          </el-tooltip>
          <!-- 删除 -->
          <el-tooltip
            class="item"
            effect="dark"
            content="编辑"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="deleteBlogTypeHandle(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹出框 -->
    <el-dialog title="编辑文章分类" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="分类名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="排序等级">
          <el-select v-model="form.order" placeholder="请选择活动区域">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
            <el-option label="4" value="4"></el-option>
            <el-option label="5" value="5"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmEdifBlogTypeHandle"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getBlogType,
  addBlogType,
  deleteBlogType,
  getOneBlogType,
  updateOneBlogType,
} from "@/api/blogType";
export default {
  data() {
    return {
      input: "",
      select: "5",
      listLoading: false,
      data: [],
      isExist: false, //判断分类是否已经存在
      dialogFormVisible: false,
      form: {
        name: "",
        order: "",
      },
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getBlogType().then((res) => {
        this.listLoading = false;
        this.data = res.data;
        // console.log(this.data);
      });
    },
    // 添加文章分类
    addBlogTypeHandle() {
      this.isExist = false;
      if (this.input) {
        let value = this.input.toUpperCase();
        // console.log("将用户的输入值大写", value); //将input 的值转换为大写
        for (const item of this.data) {
          let server_value = item.name.toUpperCase();
          // console.log("将服务器所有的字母都大写", server_value);
          if (value === server_value) {
            this.$message({
              type: "error",
              message: "该分类已存在!!!,不可重复添加",
            });
            this.isExist = true;
            break;
          } else {
            this.isExist = false;
          }
        }
        if (!this.isExist) {
          // 向服务器发送请求
          addBlogType({
            name: this.input,
            order: this.select,
          }).then(() => {
            this.fetchData();
            this.$message({
              type: "success",
              message: "添加成功",
            });
          });
        }
      } else {
        this.$message({
          type: "warning",
          message: "请输入要添加的分类,后在点击添加按钮",
        });
      }
    },
    // 编辑文章分类
    editBlogTypeHandle({ id }) {
      getOneBlogType(id).then((res) => {
        this.form = res.data;
        this.dialogFormVisible = true;
      });
    },
    // 编辑分类弹出框 确定按钮
    confirmEdifBlogTypeHandle() {
      updateOneBlogType({
        id: this.form.id,
        data: this.form,
      }).then((res) => {
        console.log(res);
        this.fetchData();
        this.dialogFormVisible = false;
        this.$message.success("修改成功");
      });
    },
    // 删除文章分类
    deleteBlogTypeHandle({ id }) {
      this.$confirm(
        "删除该分类后，该分类下面的所有文章将变为未分类状态",
        "是否删除此文章分类",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          // 发送删除信息请求
          deleteBlogType(id).then((res) => {
            console.log(res);
            this.fetchData();
            this.$message.success("删除成功");
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
.blogAddInput {
  width: 500px;
  margin-bottom: 20px;
  margin-right: 15px;
}
</style>